﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Web;

namespace WQ.Common
{
    public static class Utility
    {
        public static string ConvertAbsolutePath(string virtualPath, string weburl)
        {
            return ConvertAbsolutePath(virtualPath, weburl, false);
        }

        public static string ConvertAbsolutePath(string virtualPath, string weburl, bool systemEncode)
        {
            if (string.IsNullOrEmpty(weburl))
            {
                return string.Empty;
            }
            if (weburl.Contains("://") || weburl.StartsWith("/", StringComparison.Ordinal))
            {
                return weburl;
            }
            if (systemEncode)
            {
                return (virtualPath + HttpUtility.UrlEncode(weburl));
            }
            return (virtualPath + DataSecurity.UrlEncode(weburl));
        }

        public static IPAddress GetHostIP()
        {
            return GetHostIP(string.Empty);
        }

        public static IPAddress GetHostIP(string hostName)
        {
            IPAddress[] hostAddresses = Dns.GetHostAddresses(hostName);
            IPAddress none = IPAddress.None;
            foreach (IPAddress address2 in hostAddresses)
            {
                if (address2.AddressFamily == AddressFamily.InterNetwork)
                {
                    return address2;
                }
            }
            return none;
        }

        public static string GetPageNameFromUrl(string weburl)
        {
            string fileName = string.Empty;
            if (string.IsNullOrEmpty(weburl))
            {
                return fileName;
            }
            fileName = Path.GetFileName(weburl);
            if (string.IsNullOrEmpty(fileName))
            {
                return fileName;
            }
            int index = fileName.IndexOf('.');
            return fileName.Substring(0, index);
        }

        public static string GetSubDomainNameFromUrl(Uri siteUrl)
        {
            string virtualPath = string.Empty;
            if (siteUrl != null)
            {
                virtualPath = siteUrl.Scheme + "://" + siteUrl.DnsSafeHost;
                if (!siteUrl.IsDefaultPort)
                {
                    virtualPath = virtualPath + ":" + siteUrl.Port;
                }
            }
            return VirtualPathUtility.AppendTrailingSlash(virtualPath);
        }

        public static string ServerMapPath(string path)
        {
            if (string.Compare(path.Substring(0, 2), "~/", StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new Exception("参数错误！");
            }
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                path = current.Server.MapPath(path);
                return path;
            }
            path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path.Substring(2));
            return path;
        }
    }

}
